package com.zrrd.jt_admin.image.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zrrd.jt_admin.common.entity.Image;
import com.zrrd.jt_admin.common.vo.ImagePageVo;
import com.zrrd.jt_admin.file.service.FileStorageService;
import com.zrrd.jt_admin.image.mapper.ImageMapper;
import com.zrrd.jt_admin.image.service.ImageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.minio.RemoveObjectArgs;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ZhangYK
 * @since 2024-07-03
 */
@Slf4j
@Service
public class ImageServiceImpl extends ServiceImpl<ImageMapper, Image> implements ImageService {

    @Resource
    private FileStorageService fileStorageService;

    @Value("${minio.endpoint}")
    private String endpoint;

    @Value("${minio.bucket}")
    private String bucket;

    /**
     * 上传图片
     * @param file
     * @param imageClassId
     * @return
     */
    @Override
    public Image upload(MultipartFile file, Integer imageClassId) {
        try {
            InputStream inputStream = file.getInputStream();
            String filename = file.getOriginalFilename();
            String imageFile = fileStorageService.uploadImageFile("", filename, inputStream);
            //把图片信息封装成image对象
            Image image = new Image();
            image.setImageClassId(imageClassId);
            image.setName(imageFile.substring(imageFile.lastIndexOf("/") + 1));
            image.setPath(imageFile);
            image.setUrl(endpoint+"/"+bucket+imageFile);
            log.info(image.toString());
            return image;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 删除图片
     * @param imageId
     * @return
     */
    @Override
    public Boolean remove(Integer imageId) {
        Image image = getById(imageId);
        //删除MinIO中的文件
        fileStorageService.deleteFile(image.getPath());
        //删除数据库里的图片数据
        boolean flag = removeById(imageId);
        return flag;
    }

    /**
     * 分页查询
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public ImagePageVo findByPage(Integer pageNo, Integer pageSize) {
        //分页
        PageHelper.startPage(pageNo, pageSize);

        ImagePageVo imagePageVo = new ImagePageVo();

        List<Image> list = list();

        PageInfo<Image> pageInfo = new PageInfo<>(list);

        imagePageVo.setList(list);
        imagePageVo.setTotalCount(pageInfo.getTotal());
        return imagePageVo;
    }

    /**
     * 批量删除照片
     * @param imageIds
     * @return
     */
    @Override
    public Boolean removes(List<Integer> imageIds) {
        //List<RemoveObjectArgs> list = new ArrayList<>();
        for (Integer imageId : imageIds) {
           remove(imageId);
        }
        return true;
    }
}
